﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.Collapse
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FuzzyGroupingComponent:WorkflowComponentBase
    {
        #region fields
        private FuzzyGroupingInstruction _Instruction;
        private FuzzyGroupingOutput _Output;
        #endregion

        #region props
        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction = (FuzzyGroupingInstruction) value; }
        }

        public override ResultBase Output
        {
            get
            {
                return this._Output;
            }
            set { this._Output = (FuzzyGroupingOutput) value; }
        }

        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._Instruction.ConnStr;
            }
            set
            {
                this._Instruction.ConnStr = value;
            }
        }

        /// <summary></summary>
        public string TableName
        {
            get
            {
                return this._Instruction.TableName;
            }
            set
            {
                this._Instruction.TableName = value;
            }
        }

        /// <summary></summary>
        public string IDFieldName
        {
            get
            {
                return this._Instruction.IDFieldName;
            }
            set
            {
                this._Instruction.IDFieldName = value;
            }
        }

        /// <summary></summary>
        public string FieldName
        {
            get
            {
                return this._Instruction.FieldName;
            }
            set
            {
                this._Instruction.FieldName = value;
            }
        }

        /// <summary></summary>
        public double SimilarityThreshold
        {
            get
            {
                return this._Instruction.SimilarityThreshold;
            }
            set
            {
                this._Instruction.SimilarityThreshold = value;
            }
        }

        /// <summary></summary>
        public bool PersistToTable
        {
            get
            {
                return this._Instruction.PersistToTable;
            }
            set
            {
                this._Instruction.PersistToTable = value;
            }
        }

        /// <summary></summary>
        public string OutputTableName
        {
            get
            {
                return this._Instruction.OutputTableName;
            }
            set
            {
                this._Instruction.OutputTableName = value;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public FuzzyGroupingComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.ComponentName = "Fuzzy Grouping";
            this.Description = "Collapse field based on MS fuzzy grouping algorithm";
            this.ComponentIcon = Resources.TxGroupDups_201;
            this.ComponentImage = Resources.tour;

            this._Instruction = new FuzzyGroupingInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty,
                                                             string.Empty, string.Empty, 0.8, false, string.Empty);
            this._Output = new FuzzyGroupingOutput(this.ComponentID, Guid.Empty, new Dictionary<int, int>(),
                                                   new Dictionary<int, double>(), 0);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IDFieldName", this.IDFieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FieldName", this.FieldName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SimilarityThreshold", this.SimilarityThreshold.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PersistToTable", this.PersistToTable.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputTableName", this.OutputTableName);

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FuzzyGroupingComponent component = base.Instantiate(xNode) as FuzzyGroupingComponent;

                component.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                component.TableName = XmlDataUtil.GetAttributeValue(xNode, "TableName", "");
                component.IDFieldName = XmlDataUtil.GetAttributeValue(xNode, "IDFieldName", "");
                component.FieldName = XmlDataUtil.GetAttributeValue(xNode, "FieldName", "");
                component.SimilarityThreshold =
                    double.Parse(XmlDataUtil.GetAttributeValue(xNode, "SimilarityThreshold", "0.8"));
                component.PersistToTable =
                    bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "PersistToTable", false.ToString()));
                component.OutputTableName = XmlDataUtil.GetAttributeValue(xNode, "OutputTableName", "");

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate email: invalid type specified in xml node");
            }
        }
    }
}
